import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "aa",
      home: MyHttpPage(),
    );
  }
}

class MyHttpPage extends StatefulWidget {
  @override
  _MyHttpPageState createState() => _MyHttpPageState();
}

class _MyHttpPageState extends State<MyHttpPage> {
  var http_data = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("fd"),
      ),
      body: Text("$http_data"),
      floatingActionButton:
          FloatingActionButton(onPressed: onInCrease, child: Icon(Icons.add)),
    );
  }

  void getHttp() async {
    try {
      Response response = await Dio().get("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ");
      print(response.data.toString());
//      var jsonData=json.decode(response.data.toString());
//      print(jsonData);

      print(json.encode(User("aa","bb")));

      String jsonStr='[{"name":"Jack"},{"name":"Rose"}]';
      var  jsonData=json.decode(jsonStr);
      print(jsonData);
      setState(() {
        http_data = jsonData[0]["name"];
      });
    } catch (e) {
      print(e);
    }
  }

  void onInCrease() {
    getHttp();
  }
}


class User {
  final String name;
  final String email;

  User(this.name, this.email);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
      <String, dynamic>{
        'name': name,
        'email': email,
      };
}